package com.wnhz.work.yz.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 添加银行卡
 */
public class BankAddActivity extends BaseActivity implements View.OnClickListener {

    private EditText user_name_ed, bank_number_ed, bank_name, bank_name_child, phone_number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_add);
        initTitle();
        user_name_ed = (EditText) findViewById(R.id.user_name_ed);
        bank_number_ed = (EditText) findViewById(R.id.bank_number_ed);
        bank_name = (EditText) findViewById(R.id.bank_name);
        bank_name_child = (EditText) findViewById(R.id.bank_name_child);
        phone_number = (EditText) findViewById(R.id.phone_number);
    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("添加银行卡");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.btn_main_right).setOnClickListener(this);
        findViewById(R.id.btn_main_right).setVisibility(View.VISIBLE);
        TextView right_tv = (TextView) findViewById(R.id.tv_right);
        right_tv.setVisibility(View.VISIBLE);
        right_tv.setText("提交");
        right_tv.setTextColor(getResources().getColor(R.color.main_color));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.btn_main_right:
                if (bank_number_ed.getText().toString().trim().equals("")) {
                    MyToast("银行卡号不能为空");
                    return;
                }
                if (bank_name.getText().toString().trim().equals("")) {
                    MyToast("银行名不能为空");
                    return;
                }
                if (bank_name_child.getText().toString().trim().equals("")) {
                    MyToast("开户支行名不能为空");
                    return;
                }
                if (user_name_ed.getText().toString().trim().equals("")) {
                    MyToast("开户姓名不能为空");
                    return;
                }
                if (phone_number.getText().toString().trim().equals("")) {
                    MyToast(" 预留手机号不能为空");
                    return;
                }
                addBank();
                break;
        }
    }

    private void addBank() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
//        参数 token 和 bank_no       银行卡号
//        bank_name     银行名
//        bank_branch   开户支行名
//        username      开户姓名
//        telephone     预留手机号*/
        map.put("bank_no", bank_number_ed.getText().toString().trim());
        map.put("bank_name", bank_name.getText().toString().trim());
        map.put("bank_branch", bank_name_child.getText().toString().trim());
        map.put("username", user_name_ed.getText().toString().trim());
        map.put("telephone", phone_number.getText().toString().trim());


        showDialog();
      /*  Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }*/
        XUtil.Post(Url.MONEY_ADDBANKCARD, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        finish();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }
}
